<template>
  <view v-if="shenheStatus == 0" class="allorder">
    <scroll-view
      :scroll-y="true"
      @scrolltolower="scrollPageBottom"
      style="height: 100vh"
    >
      <template v-if="orderList.length > 0">
        <!-- 列表渲染订单 -->
        <view v-for="(orderitem, orderindex) in orderList" :key="orderindex">
          <orderItem
            :orderitem="orderitem"
            :orderindex="orderindex"
            :user_id="routeOptions.scene"
            @toOrder="toOrder"
            @handleConfirm="handleConfirm"
            @handleCancle="handleCancle"
          ></orderItem>
        </view>
      </template>
      <u-empty :text="$t('common.noDeal')" mode="list" v-else></u-empty>
    </scroll-view>
    <login-modal></login-modal>
  </view>
  <Shenhe v-else-if="shenheStatus == 1"></Shenhe>
</template>

<script>
import Shenhe from '@/components/shenhe/shenhe';
import orderItem from './components/order-item.vue';

export default {
  components: {
    orderItem,
    Shenhe,
  },
  data() {
    return {
      orderList: [],
      page: 1,
      totalPage: 0,
      routeOptions: {},
    };
  },
  onLoad(t) {
    this.routeOptions = t;

    const token = uni.getStorageSync('token');
    if (token) {
      this.cherkOrderList({
        user_id: this.routeOptions.scene,
        page: this.page,
      });
    }
  },
  onShow() {},
  watch: {},
  methods: {
    // 待核销列表
    cherkOrderList(params) {
      uni.showLoading({
        title: this.$t('home.loading'),
      });
      this.$allrequest.cherk
        .cherkOrderList(params)
        .then(res => {
          if (!res.code && res.data) {
            if (params.loadmMore) {
              this.orderList = [...this.orderList, ...res.data.list];
            } else {
              this.orderList = res.data.list;
            }
            this.totalPage = res.data.page_count;
          } else {
            uni.showModal({
              title: this.$t('order.prompt'),
              content: res.msg,
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  this.$utils.toUrl('/pages/home/home', 'switchtab');
                }
              },
            });
          }
        })
        .then(() => {
          uni.hideLoading();
        });
    },
    // 滚动至列表底部
    scrollPageBottom() {
      this.page++;
      if (this.page > this.totalPage) return;
      this.cherkOrderList({
        user_id: this.routeOptions.scene,
        page: this.page,
        loadmMore: true,
      });
    },
    // 跳转订单详情
    toOrder(index) {
      var order = this.orderList[index.index];
      uni.navigateTo({
        url: '/order/order-detail/order-detail?id=' + order.id,
      });
    },
    // 核销订单
    writeOffOrder(params) {
      this.$allrequest.cherk.cherkOrder(params).then(res => {
        if (!res.code) {
          for (let i = 0; i < this.orderList.length; i++) {
            if (this.orderList[i].id == params.id) {
              this.orderList[i].hasClerk = true;
              this.orderList[i].is_clerk = 1;
              break;
            }
          }
          uni.showToast({
            title: this.$t('mdAdmin.clerkSuc'),
            duration: 2000,
            icon: 'none',
            mask: true,
          });
        } else {
          uni.showToast({
            title: res.msg,
            duration: 2000,
            icon: 'none',
            mask: true,
          });
        }
      });
    },
    // 订单详情组件点击确认按钮
    handleConfirm(e) {
      if (e.type == '核销订单') {
        uni.showModal({
          title: this.$t('order.prompt'),
          content: this.$t('user.clerkOrder'),
          success: res => {
            if (res.confirm) {
              this.writeOffOrder({ id: e.id, type: e.item.type });
            } else if (res.cancel) {
            }
          },
        });
      }
    },
    // 订单详情组件点击取消按钮
    handleCancle(e) {
      if (e.type == '删除订单') {
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.allorder {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: #f2f2f2;
}

.scroll-item {
  width: 100%;
  height: 100%;
}
</style>
